$(function () {

    var queryUrl = '../toolscheckplanitem/list';
    var planId = getQueryString('planId');
    if(planId){
        vm.planId = planId;
        queryUrl += '?planId=' + planId;
    }
    vm.devLineId = getQueryString('devLineId');
    vm.status = getQueryString('status');
    $("#jqGrid").Grid({
        url: queryUrl,
        colModel: [
            {label: '生产线', index: 'DEV_ID',
                formatter: function (value, col, row) {
                    return row.devProductCode + ' ' + row.devProductName;
                }
            },
            {label: '使用工序', index: 'DEV_ID',
                formatter: function (value, col, row) {
                    return row.devProceNo + ' ' + row.devProceName;
                }
            },
            {label: '设备名称', name: 'devName', index: 'DEV_ID'},
            {label: '设备编号', name: 'devCode', index: 'DEV_ID'},
            {label: '实施月份', name: 'planMonth', index: 'PLAN_MONTH',editable:true},
            {
                label: '关联工装', width: 40, sortable: false,
                formatter: function (value, col, row) {
                    return "<button class='btn btn-outline btn-info' id='recordsText' onclick='vm.viewRelToolsList(\"" + row.id + "\")'><i class='fa fa-file-text-o'></i></button>";
                }
            },
            {label: '检测计划ID', name: 'planId', hidden: true},
            {label: 'ID', name: 'id', index: 'ID', hidden: true}
        ],
        forceFit : true,
        cellEdit: true,
        cellsubmit: 'remote',
        cellurl : '../toolscheckplanitem/update',
        afterSaveCell: function (rowid, name, val, iRow, iCol) {
            vm.reload();
        },
        afterSubmitCell: function (serverresponse, rowid, cellname, value, iRow, iCol) {
            var responseJson = JSON.parse(serverresponse.responseText);
            if(responseJson.code == 0){
                return [true, "成功"]
            }else{
                return [false, responseJson.msg];
            }
        }
    });
});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        planId: '', // 工装检测计划ID
        devLineId: '', // 产线ID
        status: '', // 工装检测计划状态
        toolsCheckPlanItem: {remark: '', gmtModified: '', gmtCreate: '', devId: '', planId: '', id: '',},
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            name: ''
        }
    },
    methods: {
        viewRelToolsList: function (itemId) {
            openWindow({
                type: 2,
                title: '<i class="fa fa-list-ol"></i> 工装周期性检测计划明细',
                content: '../tools/toolscheckplanitemtools.html?itemId=' + itemId + '&status=' + vm.status + '&productLine=' + vm.devLineId,
                btn: ['取消'],
            })
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            openWindow({
                type: 2,
                title: '<i class="fa fa-file-text-o"></i> 选择设备',
                content: '../tools/toolsDeviceSelect.html?deptCode=' + vm.devLineId + '&toolsCheckPlanId=' + vm.planId,
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var iframeWin = window["layui-layer-iframe" + index];
                    var res = iframeWin.getDeviceInfo();
                    if (res.length == 0) {
                        iview.Message.error("请选择一条记录");
                    } else {
                        var param = {planId: vm.planId , devIdList:res};
                        Ajax.request({
                            url: "../toolscheckplanitem/save",
                            params: JSON.stringify(param),
                            type: "POST",
                            contentType: "application/json",
                            successCallback: function (r) {
                                alert('操作成功', function (index) {

                                    vm.reload();
                                });
                            }
                        });
                    }
                    layer.close(index);
                }
            })
        },
        update: function (event) {
            let devId = getSelectedRow("#jqGrid");
            if (devId == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(devId);
        },
        saveOrUpdate: function (event) {
            let url = vm.toolsCheckPlanItem.devId == (null || '') ? "../toolscheckplanitem/save" : "../toolscheckplanitem/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.toolsCheckPlanItem),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            let devIds = getSelectedRows("#jqGrid");
            if (devIds == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../toolscheckplanitem/delete",
                    params: JSON.stringify(devIds),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (devId) {
            Ajax.request({
                url: "../toolscheckplanitem/info/" + devId,
                async: true,
                successCallback: function (r) {
                    vm.toolsCheckPlanItem = r.toolsCheckPlanItem;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
        },
        reloadSearch: function () {
            vm.q = {
                name: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        }
    }
});
